package org.ellwood.core.common.valid.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.ellwood.core.common.valid.annotation.NotZero;

/**
 * @ProjectName: ellwood-boot-pro
 * @PackageName: org.ellwood.core.common.valid.annotation
 * @ClassName: NotZeroValidator
 * @Description: // 非零验证器
 * @Author: Ellwood
 * @CreateDate: 2024/6/18 - 下午12:02
 * @Version: V1.0.0
 */
public class NotZeroValidator implements ConstraintValidator<NotZero,Number> {

    @Override
    public void initialize(NotZero constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(Number value, ConstraintValidatorContext constraintValidatorContext) {
        if (value == null) {
            return false;
        }

        if (value instanceof Integer) {
            return value.intValue() != 0;
        }

        if (value instanceof Long) {
            return value.longValue() != 0L;
        }
        return true;
    }

}
